import Taro, {Component} from '@tarojs/taro';
import {View, Text, Input,Button} from '@tarojs/components';
import {connect} from '@tarojs/redux';
import Head from '../../components/head/head';
import {userAccessToken} from '../../actions/user';

import './login.less';

@connect(function(store){
    return {user: store.user}
},function(dispatch){
    return {userAccessToken(params){
        return dispatch(userAccessToken(params))
    }}
})
class Login extends Component {
    config={
        navigationBarTitleText: '登录'
    }
    changeToken(event) {
        if (event && event.target) {
            this.setState({token: event.target.value});
        }
    }
    // 验证token
    loginToken() {
        if (this.state.token) {
            if (this.props.userAccessToken) {
                this.props.userAccessToken({accesstoken: this.state.token}).then(result => {
                    //console.log('验证成功')
                    Taro.redirectTo(url,'/pages/user/index')
                })
            }

        } else {
            Taro.showToast({title: '请输入密钥再进行登录'})
        }
    }
    render() {
        return (<View className='login-body'>
            <Head />
            <View className='form'>
                <Input onInput={this.changeToken.bind(this)} className='access_input' placeholder='请输入accesstoken' />
                <Button onClick={this.loginToken.bind(this)} className='btn_login'>登录</Button>
            </View>

        </View>)
    }
}

export default Login;